//
//  ViewController.m
//  Child
//
//  Created by Justsoso on 16/3/17.
//  Copyright © 2016年 Justsoso. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
{
    CGPoint pointXY;
}
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    NSMutableArray *mArr = [[NSMutableArray alloc] init];
    for (int i=1; i<=2; i++)
    {
        NSString *picName = [NSString stringWithFormat:@"chara%i.png",i];
        UIImage *image = [UIImage imageNamed:picName];
        [mArr addObject:image];
    }
    self.imageView.animationImages = mArr;
    self.imageView.animationDuration = 0.2;
    
    
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint p = [touch locationInView:self.view];
    pointXY = CGPointMake(p.x - self.imageView.center.x, p.y-self.imageView.center.y);
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event
{
    [self.imageView startAnimating];
    
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self.view];
    
    self.imageView.center = CGPointMake(point.x - pointXY.x, point.y - pointXY.y);
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(nullable UIEvent *)event
{
    [self.imageView stopAnimating];

}


@end
